﻿
using System.Configuration;
using VTCO.Utils;
using System.Collections.Specialized;
using System.Linq;
namespace Utils
{
    public class Setting
    {
        /// <summary>
        /// Chuỗi kết nối CSDL
        /// </summary>
        public static string ConnectionString
        {
            get
            {
               //RijndaelEnhanced rij = new RijndaelEnhanced("QLCF!()%");
                return @"Data Source=" + ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString + ";Initial Catalog=QLCF;Integrated Security=True"; 
               //return rij.Decrypt(ConfigurationManager.ConnectionStrings["CONNECTION_STRING_QLCF"].ConnectionString);
            }
        }
        public static string ServerName
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString;
            }
        }

        /// <summary>
        /// Lấy giá trị của Key trong AppSetting
        /// </summary>
        /// <param name="_Key">Key</param>
        /// <returns>Return Null nếu không có key</returns>
        public static string GetAppKeyValue(string _Key)
        {
            NameValueCollection appsettings = ConfigurationManager.AppSettings;
            if (appsettings == null) return null;
            if (appsettings.AllKeys.Contains(_Key))
            {
                return appsettings[_Key];
            }
            else
            {
                return null;
            }
        }

        public static int SetAppKeyValue(string _Key, string _value)
        {
            try
            {
                NameValueCollection appsettings = ConfigurationManager.AppSettings;
                appsettings.Set(_Key, _value);
                //NameValueCollection appsettings = ConfigurationManager.AppSettings;
                //if (appsettings == null) return 0;
                //if (appsettings.AllKeys.Contains(_Key))
                //{
                //    appsettings[_Key] = _value;
                //    return 1;
                //}
                //else
                //{
                //    return 0;
                //}
                // Open App.Config of executable
                //System.Configuration.Configuration config =
                // ConfigurationManager.OpenExeConfiguration
                //            (ConfigurationUserLevel.None);

                //// Add an Application Setting.
                //config.AppSettings.Settings.Add(_Key, _value);

                //// Save the changes in App.config file.
                //config.Save(ConfigurationSaveMode.Modified);

                //// Force a reload of a changed section.
                //ConfigurationManager.RefreshSection("appSettings");
                return 1;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Lấy xâu kết nối trong file Web.Config
        /// </summary>
        /// <param name="_Key">key</param>
        /// <returns>Return null nếu không có key</returns>
        public string GetConnectionStringKeyValue(string _Key)
        {
            try
            {                
                RijndaelEnhanced rij = new RijndaelEnhanced("QLCF!()%");
                return rij.Decrypt(ConfigurationManager.ConnectionStrings[_Key].ConnectionString);
            }
            catch { }
            return null;
        }

        public string MaHoa(string _Key)
        {
            try
            {
                RijndaelEnhanced rij = new RijndaelEnhanced("QLCF!()%");
                return rij.Encrypt(ConfigurationManager.ConnectionStrings[_Key].ConnectionString);
            }
            catch { }
            return null;
        }
    }
}
